# delimit ;
clear all;
set more 1;
set maxvar 32000;
version 12;
local seed = 735;

*******************************************************************************;
*directories;
*******************************************************************************;

*Define the main directory;
global upper_dir = "";

global scratch_dir = "$upper_dir\scratch";

global paper_dir = "$upper_dir\tables_figures";

global nlsy_data_dir = "$upper_dir\data";

global dofile_dir = "$upper_dir\stata_code";

local do_here =1;

if `do_here'==1{;

*load the estimated parameters;
cd "$scratch_dir";
use boot_collect_ols0_restricted0_new.dta, clear;

*keep only point estimates;
keep if boot_sample==0;

drop boot_sample blah do_ols restricted_model;


*Store estimates in matrix for the simulation;

*Initial Conditions;
matrix V = ( var_child_cog[1]            ,   cov_child_cog_mom_cog[1]  ,  cov_child_cog_mom_noncog[1] ,  cov_child_cog_log_inc[1] \
			 cov_child_cog_mom_cog[1]    ,   var_mom_cog[1]            ,  cov_mom_cog_mom_noncog[1]   ,  cov_mom_cog_log_inc[1] \  
			 cov_child_cog_mom_noncog[1] ,	 cov_mom_cog_mom_noncog[1] ,  var_mom_noncog[1]           ,  cov_mom_noncog_log_inc[1] \  
             cov_child_cog_log_inc[1]    ,   cov_mom_cog_log_inc[1]    ,  cov_mom_noncog_log_inc[1]   ,  var_faminc5[1]   );

matrix m = (0, 0 , 0, mean_income_age5[1]); 

*Technology (order of variables: TFP, child's skills, investments, interaction term, shocks);
matrix gamma = J(5,4,.);

local t_ind = 1;
forvalues t= 5(2)11{;

matrix gamma[1,`t_ind'] = log_TFP_age`t'[1];
matrix gamma[2,`t_ind'] = gamma1_age`t'[1];
matrix gamma[3,`t_ind'] = gamma2_age`t'[1];
matrix gamma[4,`t_ind'] = gamma3_age`t'[1];
matrix gamma[5,`t_ind'] = std_prod_age`t'[1];

local ++t_ind;

};
matrix list gamma;

*Investment Equation (order of variables: child's skills, mom cognitive skills, mom noncognitive skills, family income, shocks);
matrix alpha = J(5,4,.);
local t_ind = 1;
forvalues t= 5(2)11{;

matrix alpha[1,`t_ind'] = alpha1_age`t'[1];
matrix alpha[2,`t_ind'] = alpha3_age`t'[1];
matrix alpha[3,`t_ind'] = alpha4_age`t'[1];
matrix alpha[4,`t_ind'] = alpha2_age`t'[1];
matrix alpha[5,`t_ind'] = std_inv_age`t'[1];

local ++t_ind;

};

matrix list alpha;

*Law of motion of family income;
matrix param_income = J(3,1,.);
matrix param_income[1,1] = inc_beta_const[1];
matrix param_income[2,1] = inc_beta_tm2[1];
matrix param_income[3,1] = income_shock_std[1];

***************************;
*Measurement Parameters;
***************************;

*Children's Skills;
matrix mu_skills     = J(3,5,.);
matrix lambda_skills = J(3,5,.);
matrix var_cogn      = J(3,5,.);
local t_ind = 1;
forvalues t= 5(2)13{;
matrix mu_skills[1,`t_ind'] = mu_math`t'[1];
matrix mu_skills[2,`t_ind'] = mu_recog`t'[1];
matrix mu_skills[3,`t_ind'] = mu_comp`t'[1];

matrix lambda_skills[1,`t_ind'] = lambda_math`t'[1];
matrix lambda_skills[2,`t_ind'] = lambda_recog`t'[1];
matrix lambda_skills[3,`t_ind'] = lambda_comp`t'[1];

matrix var_cogn[1,`t_ind'] = var_cogn_math`t'[1];
matrix var_cogn[2,`t_ind'] = var_cogn_recog`t'[1];
matrix var_cogn[3,`t_ind'] = var_cogn_comp`t'[1];



local ++t_ind;

};
 
matrix list mu_skills;
matrix list lambda_skills; 
matrix list var_cogn;


*Mothers' Skills;
matrix mu_mom_cog    = J(6,1,.);
matrix mu_mom_noncog = J(13,1,.);
matrix lambda_mom_cog    = J(6,1,.);
matrix lambda_mom_noncog = J(13,1,.);
matrix var_mom_cog    = J(6,1,.);
matrix var_mom_noncog = J(13,1,.);


local mom_cogn_list = "asvab2 asvab3 asvab4 asvab5 asvab6 asvab8";

local ind_var = 1;
foreach x in `mom_cogn_list'{;
matrix mu_mom_cog[`ind_var',1] = mu_`x'[1];
matrix lambda_mom_cog[`ind_var',1] = lambda_`x'[1];
matrix var_mom_cog[`ind_var',1] = var_mc_`x'[1];

local ++ind_var;
};


local mom_noncogn_list = "se1 se2 se3 se4 se5 se6 se8 se9 se10 rotter1 rotter2 rotter3 rotter4";


local ind_var = 1;
foreach x in `mom_noncogn_list'{;
matrix mu_mom_noncog[`ind_var',1] = mu_`x'[1];
matrix lambda_mom_noncog[`ind_var',1] = lambda_`x'[1];
matrix var_mom_noncog[`ind_var',1] = var_mnc_`x'[1];

local ++ind_var;
};

matrix list mu_mom_cog;
matrix list lambda_mom_cog;
matrix list mu_mom_noncog;
matrix list lambda_mom_noncog;
matrix list var_mom_cog;
matrix list var_mom_noncog;


*Investments;

matrix mu_inv     = J(2,4,.);
matrix lambda_inv = J(2,4,.);

matrix mu_inv[1,1] = mu_often_mom_reads5[1]; 
matrix mu_inv[2,1] = mu_often_museum5[1]; 
matrix mu_inv[1,2] = mu_often_mom_reads7[1]; 
matrix mu_inv[2,2] = mu_often_museum7[1]; 
matrix mu_inv[1,3] = mu_often_museum9[1]; 
matrix mu_inv[2,3] = mu_often_praised9[1]; 
matrix mu_inv[1,4] = mu_often_praised11[1]; 
matrix mu_inv[2,4] = mu_often_museum11[1];

matrix lambda_inv[1,1] = lambda_often_mom_reads5[1]; 
matrix lambda_inv[2,1] = lambda_often_museum5[1]; 
matrix lambda_inv[1,2] = lambda_often_mom_reads7[1]; 
matrix lambda_inv[2,2] = lambda_often_museum7[1]; 
matrix lambda_inv[1,3] = lambda_often_museum9[1]; 
matrix lambda_inv[2,3] = lambda_often_praised9[1]; 
matrix lambda_inv[1,4] = lambda_often_praised11[1]; 
matrix lambda_inv[2,4] = lambda_often_museum11[1];


matrix list mu_inv;
matrix list lambda_inv;


************************************************************************************************;
************************************************************************************************;
************************************************************************************************;
*Simulate Data for the Measure of Children Skills, Mother Skills, Investments and Family Income;
************************************************************************************************;
************************************************************************************************;
************************************************************************************************;

*******************************************************************************;
*Draw Initial Conditions of Latent Factors;
*******************************************************************************;
clear;
set obs 100000;
set seed `seed';
drawnorm child_cog_age5 mom_cog mom_noncog faminc_age5, means(m) cov(V);

*******************************************************************************;
*Simulate the Dynamics of Skills, Investments, and Family Income;
*******************************************************************************;

local t_ind = 1;
forvalues t= 5(2)11{;

*Investments;
gen inv_shocks_age`t' = rnormal(0,1);
qui sum inv_shocks_age`t';
replace inv_shocks_age`t' = (inv_shocks_age`t' - r(mean))/r(sd);
gen inv_age`t' = alpha[1,`t_ind']*child_cog_age`t' + alpha[2,`t_ind']*mom_cog + alpha[3,`t_ind']*mom_noncog + alpha[4,`t_ind']*faminc_age`t' + alpha[5,`t_ind']*inv_shocks_age`t';

*Skills Dynamics;
gen skills_shocks_age`t' = rnormal(0,1);
qui sum skills_shocks_age`t';
replace skills_shocks_age`t' = (skills_shocks_age`t' - r(mean))/r(sd);

local t_2 = `t' + 2;
gen child_cog_age`t_2' = gamma[1,`t_ind'] + gamma[2,`t_ind']*child_cog_age`t' + gamma[3,`t_ind']*inv_age`t'  + gamma[4,`t_ind']*child_cog_age`t'*inv_age`t' + gamma[5,`t_ind']*skills_shocks_age`t';

*Income Dynamics;
gen income_shocks_age`t' = rnormal(0,1);
qui sum income_shocks_age`t';
replace income_shocks_age`t' = (income_shocks_age`t' - r(mean))/r(sd);
gen faminc_age`t_2' = param_income[1,1] + param_income[2,1]*faminc_age`t' + param_income[3,1]*income_shocks_age`t';

local ++t_ind;

};


*******************************************************************************;
*Construct the observed measures for each latent factor;
*******************************************************************************;

*Children's Skills;
local cog_list = "math recog comp";

local ind_var = 1;
foreach x in `cog_list'{;

local t_ind = 1;
forvalues t= 5(2)13{;

gen shocks = rnormal(0,1);
qui sum shocks;
replace shocks = (shocks - r(mean))/r(sd);

gen `x'`t' = mu_skills[`ind_var',`t_ind'] + lambda_skills[`ind_var',`t_ind']*child_cog_age`t' +  shocks;

drop shocks;

local ++t_ind;
};
local ++ind_var;
};


*Mother's Skills;


local mom_cogn_list = "asvab2 asvab3 asvab4 asvab5 asvab6 asvab8";

local ind_var = 1;
foreach x in `mom_cogn_list'{;

gen shocks = rnormal(0,1);
qui sum shocks;
replace shocks = (shocks - r(mean))/r(sd);

gen `x' = mu_mom_cog[`ind_var',1] + lambda_mom_cog[`ind_var',1]*mom_cog +  shocks;

drop shocks;

local ++ind_var;

};



local mom_noncogn_list = "se1 se2 se3 se4 se5 se6 se8 se9 se10 rotter1 rotter2 rotter3 rotter4";

local ind_var = 1;
foreach x in `mom_noncogn_list'{;

gen shocks = rnormal(0,1);
qui sum shocks;
replace shocks = (shocks - r(mean))/r(sd);

gen `x' = mu_mom_noncog[`ind_var',1] + lambda_mom_noncog[`ind_var',1]*mom_noncog +  shocks;

drop shocks;

local ++ind_var;

};


local inv_list = " often_mom_reads5 often_museum5 often_mom_reads7 often_museum7 often_museum9 often_praised9 often_praised11 often_museum11";

local ind_var = 1;
local t       = 5;
foreach x in `inv_list'{;

gen shocks = rnormal(0,1);
qui sum shocks;
replace shocks = (shocks - r(mean))/r(sd);

local t_ind = round(`ind_var'/2);
local x_ind = floor(`ind_var'/2- (round(`ind_var'/2)-1) )+1;

di `x_ind';
di `t_ind';

gen `x' = mu_inv[`x_ind',`t_ind'] + lambda_inv[`x_ind',`t_ind']*inv_age`t' +  shocks;

drop shocks;
local ++ind_var;

if `x_ind'==2{;

local t = `t' + 2;

};

};
 
*keep faminc_age* math* recog* comp* asvab* se* rotter* often_* ;

cd "$scratch_dir";
save data_montecarlo.dta, replace;


};*end if do_here for generation of data;


*******************************************************************************;
*******************************************************************************;
*******************************************************************************;
*Start the Estimation Algorithm;
*******************************************************************************;
*******************************************************************************;
*******************************************************************************;

matrix alpha_hat = J(5,4,.);
matrix gamma_hat = J(5,4,.);

cd "$scratch_dir";
use data_montecarlo.dta, clear;

local mom_cog_measures = "asvab2 asvab3 asvab4 asvab5 asvab6 asvab8";
local mom_noncog_measures = "se1 se2 se3 se4 se5 se6 se8 se9 se10 rotter1 rotter2 rotter3 rotter4";

*******************************************************************************;
*factor loadings at period 5
*******************************************************************************;


gen temp1 = .;
gen temp2 = .;


***************************;
*child's cognitive
***************************;
matrix mu_skills_hat = J(3,5,.);
matrix lambda_skills_hat  = J(3,5,.);

gen lambda_math5 = 1;

correlate comp5 recog5, covariance;
replace temp1 = r(cov_12);
correlate math5 recog5, covariance;
replace temp2 = r(cov_12);

gen lambda_comp5 = temp1/temp2;

correlate comp5 recog5, covariance;
replace temp1 = r(cov_12);
correlate math5 comp5, covariance;
replace temp2 = r(cov_12);

gen lambda_recog5 = temp1/temp2;


***************************;
*mother cognitive
***************************;

local meas_norm = "asvab2";
local meas_list = "`mom_cog_measures'";

gen temp_lambda_`meas_norm' = 1;

local index = 0;
foreach Z2 in `meas_list' {;

if "`Z2'" ~= "`meas_norm'" {;
gen temp_lambda_`Z2' = .;
};

foreach Z3 in `meas_list' {;

if "`Z2'" ~= "`meas_norm'" & "`Z2'" ~= "`Z3'" & "`Z3'" ~= "`meas_norm'" {;

local index = `index' + 1;

correlate `Z2' `Z3', covariance;
replace temp1 = r(cov_12);
correlate `meas_norm' `Z3', covariance;
replace temp2 = r(cov_12);

replace temp_lambda_`Z2' = temp1 / temp2 in `index';

};
};

qui sum temp_lambda_`Z2';
gen lambda_`Z2' = r(mean);
drop temp_lambda_`Z2';
};

***************************;
*mom non-cognitive measures;
***************************;

local meas_norm = "se1";
local meas_list = "`mom_noncog_measures'";

gen temp_lambda_`meas_norm' = 1;

local index = 0;
foreach Z2 in `meas_list' {;

if "`Z2'" ~= "`meas_norm'" {;
gen temp_lambda_`Z2' = .;
};

foreach Z3 in `meas_list' {;

if "`Z2'" ~= "`meas_norm'" & "`Z2'" ~= "`Z3'" & "`Z3'" ~= "`meas_norm'" {;

local index = `index' + 1;

correlate `Z2' `Z3', covariance;
replace temp1 = r(cov_12);
correlate `meas_norm' `Z3', covariance;
replace temp2 = r(cov_12);

replace temp_lambda_`Z2' = temp1 / temp2 in `index';

};
};
qui sum temp_lambda_`Z2';
gen lambda_`Z2' = r(mean);
drop temp_lambda_`Z2';
};



*******************************************************************************;
*create tilde measures for initial factors;
*******************************************************************************;

local cog_list5 = "math5 recog5 comp5";

foreach X in `mom_cog_measures' `mom_noncog_measures' `cog_list5' {;
sum `X';
gen mu_`X' = r(mean);
gen `X'_tilde = (`X' - mu_`X') / lambda_`X';
};

gen faminc5_tilde = faminc5;


*Age-invariant Measure;
local cog_list = "math";

foreach X in `cog_list' {;
local t_ind = 2;
forvalues t = 7(2)13 {;
gen mu_`X'`t' = mu_`X'5;
gen lambda_`X'`t' = lambda_`X'5;
gen `X'`t'_tilde = (`X'`t' - mu_`X'`t') / lambda_`X'`t';
matrix mu_skills_hat[1,`t_ind'] =  mu_`X'`t'[1];
matrix lambda_skills_hat[1,`t_ind'] =  lambda_`X'`t'[1];
local ++t_ind;
};
};

matrix mu_skills_hat[1,1] =  mu_math5[1];
matrix mu_skills_hat[2,1] =  mu_recog5[1];
matrix mu_skills_hat[3,1] =  mu_comp5[1];
matrix lambda_skills_hat[1,1] =  lambda_math5[1];
matrix lambda_skills_hat[2,1] =  lambda_recog5[1];
matrix lambda_skills_hat[3,1] =  lambda_comp5[1];

******************************************************************************;
*First Stages for Measurement Error Correction;
******************************************************************************;

reg asvab2_tilde recog5 comp5 asvab3 asvab4 asvab5 asvab6 asvab8 ;
predict asvab2_tilde_hat;
reg se1_tilde recog5 comp5 se2 se3 se4 se5 se6 se8 se9 se10 rotter1 rotter2 rotter3 rotter4; 
predict se1_tilde_hat;



forvalues t = 5(2)13 {;
reg math`t'_tilde  recog`t'  comp`t';
predict math`t'_tilde_hat if e(sample);
};


local t_ind = 1;

forvalues t = 5(2)11 {; *loop of sequential estimation;


*******************************************************************************;
*PART 1: estimate investment function;
*******************************************************************************;

local mom_cog_meas = "asvab2_tilde_hat";
local mom_noncog_meas = "se1_tilde_hat";


if `t' == 5 {;

local inv_meas = "often_mom_reads5";
local inv_meas2 = "often_museum5";
local skill_meas = "math5_tilde_hat";
local inc_meas = "faminc_age5";

};

if `t' == 7 {;

local inv_meas = "often_mom_reads7";
local inv_meas2 = "often_museum7";
local skill_meas = "math7_tilde_hat";
local inc_meas = "faminc_age7";

};


if `t' == 9 {;

local inv_meas = "often_museum9";
local inv_meas2 = "often_praised9";
local skill_meas = "math9_tilde_hat";
local inc_meas = "faminc_age9";

};


if `t' == 11 {;

local inv_meas = "often_praised11";
local inv_meas2 = "often_museum11";
local skill_meas = "math11_tilde_hat";
local inc_meas = "faminc_age11";
};



***********************************************;
*estimate main part of investment function
***********************************************;
reg `inv_meas' `inc_meas'  `skill_meas' `mom_cog_meas' `mom_noncog_meas', robust;

predict predict_`inv_meas' if e(sample);
gen resid_`inv_meas' = `inv_meas' - predict_`inv_meas';

gen beta1 = _b[`skill_meas'];
gen beta2 = _b[`inc_meas'];
gen beta3 = _b[`mom_cog_meas'];
gen beta4 = _b[`mom_noncog_meas'];
gen lambda_`inv_meas' = beta1 + beta2 + beta3 + beta4;
gen mu_`inv_meas' = _b[_cons];

forvalues j = 1(1)4 {;
gen alpha`j'_age`t'_1 = beta`j' / lambda_`inv_meas';
};

sum alpha*;



drop beta*;


**************************************;
*compute variance of investment shock;
**************************************;

reg `inv_meas2' `inc_meas'  `skill_meas' `mom_cog_meas' `mom_noncog_meas', robust;
predict predict_`inv_meas2' if e(sample);
gen resid_`inv_meas2' = `inv_meas2' - predict_`inv_meas2';

gen beta1 = _b[`skill_meas'];
gen beta2 = _b[`inc_meas'];
gen beta3 = _b[`mom_cog_meas'];
gen beta4 = _b[`mom_noncog_meas'];
gen mu_`inv_meas2' = _b[_cons];
gen lambda_`inv_meas2' = beta1 + beta2 + beta3 + beta4;

forvalues j = 1(1)4 {;
gen alpha`j'_age`t'_2 = beta`j' / lambda_`inv_meas2';
};

forvalues j = 1(1)4 {;
gen alpha`j'_age`t' = ( alpha`j'_age`t'_1 + alpha`j'_age`t'_2)/2;
*drop alpha`j'_age`t'_1 alpha`j'_age`t'_2;
};

drop beta*;

gen `inv_meas2'_tilde = (`inv_meas2' - mu_`inv_meas2')/ lambda_`inv_meas2';

gen resid_`inv_meas'_tilde = resid_`inv_meas' / lambda_`inv_meas';
gen resid_`inv_meas2'_tilde = resid_`inv_meas2' / lambda_`inv_meas2';

correlate resid_`inv_meas'_tilde resid_`inv_meas2'_tilde, covariance;

gen cov_inv_age`t' = r(cov_12);
gen std_inv_age`t' = sqrt(cov_inv_age`t');

matrix alpha_hat[1,`t_ind'] = alpha1_age`t'[1];
matrix alpha_hat[2,`t_ind'] = alpha3_age`t'[1];
matrix alpha_hat[3,`t_ind'] = alpha4_age`t'[1];
matrix alpha_hat[4,`t_ind'] = alpha2_age`t'[1];
matrix alpha_hat[5,`t_ind'] = std_inv_age`t'[1];

*compute tilde for investment measure;
foreach X in `inv_meas' {;
gen `X'_tilde = (`X' - mu_`X')  / lambda_`X';
};




*******************************************************************************;
*PART 2: production function: age 7 output, age 5 inputs;
*******************************************************************************;

if `t' == 5 {;

*Y variable;
local skill_meas_out = "math7";
local skill_meas_out2 = "recog7";
local skill_meas_out3 = "comp7";

*X variables;
*skills;
local skill_meas_in = "math5_tilde_hat";

*investments;
reg often_mom_reads`t'_tilde  often_museum`t';
predict often_mom_reads`t'_tilde_hat if e(sample);
local inv_meas_in = "often_mom_reads`t'_tilde_hat";

gen int_in = math5_tilde*often_mom_reads5_tilde;

*Z instruments;
gen int1 = recog5 * often_museum5;
gen int2 = comp5 * often_museum5;


reg int_in  recog5 comp5 often_museum5 int1 int2 ;
predict int_in_hat if e(sample);

local skill_inv_meas_in = "int_in_hat";

};


if `t' == 7 {;

*Y variable;
local skill_meas_out = "math9";
local skill_meas_out2 = "recog9";
local skill_meas_out3 = "comp9";

*X variables;
*skills;
local skill_meas_in = "math7_tilde_hat";

*investments;
reg often_mom_reads`t'_tilde  often_museum`t';
predict often_mom_reads`t'_tilde_hat if e(sample);
local inv_meas_in = "often_mom_reads`t'_tilde_hat";

gen int_in = math7_tilde*often_mom_reads7_tilde;


*Z instruments;
gen int1 = recog7*often_museum7;
gen int2 = comp7*often_museum7;

reg int_in recog7 comp7 often_museum7 int1 int2 ;
predict int_in_hat if e(sample);

local skill_inv_meas_in = "int_in_hat";

};


if `t' == 9 {;

*Y variable;
local skill_meas_out = "math11";
local skill_meas_out2 = "recog11";
local skill_meas_out3 = "comp11";

*X variables;
local skill_meas_in = "math9_tilde_hat";


reg often_museum`t'_tilde often_praised9;
predict often_museum`t'_tilde_hat if e(sample);
local inv_meas_in = "often_museum`t'_tilde_hat";


gen int_in = math9_tilde*often_museum9_tilde;


*Z instruments;
gen int1 = recog9 * often_praised9;
gen int2 = comp9 * often_praised9;
reg int_in recog9 comp9 often_praised9   int1 int2 ;
predict int_in_hat if e(sample);
local skill_inv_meas_in = "int_in_hat";

};


if `t' == 11 {;

*Y variable;
local skill_meas_out = "math13";
local skill_meas_out2 = "recog13";
local skill_meas_out3 = "comp13";

*X variables;
local skill_meas_in = "math11_tilde_hat";


reg  often_praised11_tilde often_museum`t';
predict often_praised11_tilde_hat if e(sample);
local inv_meas_in = "often_praised11_tilde_hat";
gen int_in = math11_tilde*often_praised11_tilde;

*Z instruments;
gen int1 = recog11 * often_museum`t';
gen int2 = comp11 * often_museum`t';
reg int_in recog11 comp11 often_museum11    int1 int2 ;
predict int_in_hat if e(sample);
local skill_inv_meas_in = "int_in_hat";

};




********************************************;
*estimate main part of production function;
********************************************;

reg `skill_meas_out'  `skill_meas_in' `inv_meas_in' `skill_inv_meas_in' , r;
predict predict_`skill_meas_out' if e(sample);
gen resid_`skill_meas_out' = `skill_meas_out' - predict_`skill_meas_out';

gen beta0 = _b[_cons];
gen beta1 = _b[`skill_meas_in'];
gen beta2 = _b[`inv_meas_in'];
gen beta3 = _b[`skill_inv_meas_in'];

forvalues j = 1(1)3 {;
gen gamma`j'_age`t' = beta`j' / lambda_`skill_meas_out';
};


***************;
*TFP;
***************;

gen log_TFP_age`t' = (beta0 - mu_`skill_meas_out') / lambda_`skill_meas_out';


*************************;
*Compute lambdas t+2 for the other two measures of children skills;
*************************;

*Second measure;
*Notice that this is equivalent to Cov(M2,M3)/Cov(M3,M1_tilde) because of the IV formula;
ivregress 2sls `skill_meas_out2' (`skill_meas_out'_tilde = `skill_meas_out3'), robust;

gen mu_`skill_meas_out2' = _b[_cons];
gen lambda_`skill_meas_out2'= _b[`skill_meas_out'_tilde];

gen `skill_meas_out2'_tilde = (`skill_meas_out2' - mu_`skill_meas_out2') / lambda_`skill_meas_out2';


*Third measure;
*Notice that this is equivalent to Cov(M3,M2)/Cov(M2,M1_tilde) because of the IV formula;
ivregress 2sls `skill_meas_out3' (`skill_meas_out'_tilde = `skill_meas_out2'), robust;

gen mu_`skill_meas_out3' = _b[_cons];
gen lambda_`skill_meas_out3'= _b[`skill_meas_out'_tilde];


gen `skill_meas_out3'_tilde = (`skill_meas_out3' - mu_`skill_meas_out3') / lambda_`skill_meas_out3';


****************************;
*variance of shock;
****************************;

gen resid_`skill_meas_out'_tilde = resid_`skill_meas_out'/lambda_`skill_meas_out';
correlate resid_`skill_meas_out'_tilde `skill_meas_out2'_tilde, covariance;
gen cov_prod_age`t' = r(cov_12);

gen std_prod_age`t' = sqrt(cov_prod_age`t');

drop beta* int*  ;



matrix gamma_hat[1,`t_ind'] = log_TFP_age`t'[1];
matrix gamma_hat[2,`t_ind'] = gamma1_age`t'[1];
matrix gamma_hat[3,`t_ind'] = gamma2_age`t'[1];
matrix gamma_hat[4,`t_ind'] = gamma3_age`t'[1];
matrix gamma_hat[5,`t_ind'] = std_prod_age`t'[1];

local ++t_ind;

};

local t_ind = 2;
forvalues t=7(2)13{;


matrix mu_skills_hat[2,`t_ind'] =  mu_recog`t'[1];
matrix mu_skills_hat[3,`t_ind'] =  mu_comp`t'[1];
matrix lambda_skills_hat[2,`t_ind'] =  lambda_recog`t'[1];
matrix lambda_skills_hat[3,`t_ind'] =  lambda_comp`t'[1];

local ++t_ind;

};



matrix temp_alpha_table = [ alpha , alpha_hat];
matrix temp_gamma_table = [ gamma , gamma_hat];
matrix temp_mu_table = [mu_skills , mu_skills_hat];
matrix temp_lambda_table = [lambda_skills , lambda_skills_hat];



matrix alpha_table = J(5,8,.);
matrix gamma_table = J(5,8,.);

forvalues i = 1(1)5{;
forvalues j = 1(1)8{;

matrix alpha_table[`i',`j'] = round(temp_alpha_table[`i',`j'], 0.001);
matrix gamma_table[`i',`j'] = round(temp_gamma_table[`i',`j'], 0.001);

};
};


matrix mu_table = J(3,10,.);
matrix lambda_table = J(3,10,.);

forvalues i = 1(1)3{;
forvalues j = 1(1)10{;

matrix mu_table[`i',`j'] = round(temp_mu_table[`i',`j'], 0.001);
matrix lambda_table[`i',`j'] = round(temp_lambda_table[`i',`j'], 0.001);


};
};



matrix list alpha_table;
matrix list gamma_table;

matrix list mu_table;
matrix list lambda_table;




stop;


























